
//Load the script which adds custom delete button 
_spBodyOnLoadFunctionNames.push("addCustomDeleteButton");  

function addCustomDeleteButton() {

    //Get the delete button object
    var button = document.getElementById("Ribbon.ListForm.Edit.Actions.DeleteItem-Large");

    if (button != null) {
        var $button = $(button);
        var html = $button.html();
        dialog = this;

        //Copy the 'delete' button and disable the original one  
        $button.after("<a href='' class='ms-cui-ctl-large'>" + html + "</a>").hide().next().click(function (e) {

            //get the id of current item
            itemID = getQuerystring('ID')

            //remove the item using Sharepoint Client Object Model
            deleteItem(itemID)

            //unbind the click event handler
            var $this = $(this);
            $this.unbind("click");
            dialog = null;
        });
    }

}

function deleteItem(itemID) {
    
    //Create current client context
    var context = new SP.ClientContext.get_current();
    //Get current web object
    var web = context.get_web();
    //Get the list where you want to delete item from
    var list = web.get_lists().getByTitle('List Title');   
    //Get the item to delete	
    var itemToDelete = list.getItemById(itemID);
    //execute delete command asynchronously
    itemToDelete.deleteObject();
    context.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failed));
}
	
function Success() {

    //TODO: Add Post-Delete Logic Here
}
	
function Failed(sender, args) {
    alert('Failed to delete the item.');
}
	
function getQuerystring(key, default_)
{
  	if (default_==null) default_=""; 
  	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  	var qs = regex.exec(window.location.href);
  	if(qs == null)
    	return default_;
  	else
    	return qs[1];
}



